package com.jamila.scancode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jamila.scancode.entity.BasicInfo;
import com.jamila.scancode.entity.Contacts;
import com.jamila.scancode.mapper.ContactsMapper;
import com.jamila.scancode.service.ContactsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author JAMILA
 * @since 2024-03-30 01:42:05
 */
@Service
public class ContactsServiceImpl extends ServiceImpl<ContactsMapper, Contacts> implements ContactsService {

    @Override
    public boolean saveData(Contacts contacts) {
        //根据手机号判断是否已存在信息；若存在则执行更新操作；若不存在则执行保存操作
        String tel = contacts.getTel();
        if (!StringUtils.hasLength(tel)){
            return false;
        }
        QueryWrapper<Contacts> wrapper=new QueryWrapper<>();
        wrapper.eq("tel",tel);
        Contacts contacts1 = baseMapper.selectOne(wrapper);
        if (contacts1==null){
            //执行保存操作
            int insert = baseMapper.insert(contacts);
            return insert>0;
        }else {
            //执行更新操作
            int update = baseMapper.updateById(contacts);
            return update>0;
        }

    }

    @Override
    public Contacts getContacts(String tel) {
        QueryWrapper<Contacts> wrapper=new QueryWrapper<>();
        wrapper.eq("tel",tel);
        Contacts contacts = baseMapper.selectOne(wrapper);
        return contacts;
    }
}
